<!DOCTYPE html>
<html>
    <head>
        <script src="/w3c/resources/testharness.js"></script>
        <script src="/w3c/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>

        <link rel='stylesheet' href='/w3c/resources/testharness.css'>
    </head>
    <body>
        <div id="log"></div>
        <script>
          function verifySourceBufferLists(mediaSource, expected)
          {
              assert_equals(mediaSource.sourceBuffers.length, expected.length, "sourceBuffers length");
              assert_equals(mediaSource.activeSourceBuffers.length, 0, "activeSourceBuffers length");
              for (var i = 0; i < expected.length; ++i)
                assert_equals(mediaSource.sourceBuffers[i], expected[i], "Verifying mediaSource.sourceBuffers[" + i + "]");
          }

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
              var sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);
              var sourceBufferB = null;

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
                  sourceBufferB = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

                  verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);
              });

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "sourceBuffers");
                  mediaSource.removeSourceBuffer(sourceBufferA);

                  verifySourceBufferLists(mediaSource, [sourceBufferB]);

                  test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
                  sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);

                  verifySourceBufferLists(mediaSource, [sourceBufferB, sourceBufferA]);
              });

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });
          }, "Test SourceBufferList event dispatching.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
              test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
              var sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);
              var sourceBufferB = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

              verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);

              test.waitForExpectedEvents(function()
              {
                  verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);

                  // Force the media element to close the MediaSource object.
                  test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "sourceBuffers");
                  test.expectEvent(mediaSource, "sourceclose", "mediaSource closing");
                  test.expectEvent(mediaElement, "error", "mediaElement error loading");
                  mediaElement.src = "";
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED, "Expected error code");
                  assert_equals(mediaSource.readyState, "closed", "mediaSource is closed.");

                  verifySourceBufferLists(mediaSource, []);
                  test.done();
              });
          }, "Test that only 1 removesourcebuffer event fires on sourceBuffers when the MediaSource closes.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              // FIXME: Test two activeSourceBuffers, either demuxed or once multitrack is supported.
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);

              test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
              test.expectEvent(mediaElement, "loadedmetadata", "loadedmetadata done.");
              sourceBuffer.appendBuffer(initSegment);

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.sourceBuffers.length, 1, "mediaSource sourceBuffers");
                  assert_equals(mediaSource.activeSourceBuffers.length, 1, "mediaSource activeSourceBuffers");

                  // Force the media element to close the MediaSource object.
                  test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "sourcebuffers");
                  test.expectEvent(mediaSource.activeSourceBuffers, "removesourcebuffer", "activesourcebuffers");
                  test.expectEvent(mediaSource, "sourceclose", "mediaSource closing");
                  test.expectEvent(mediaElement, "error", "mediaElement error loading");
                  mediaElement.src = "";
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED, "Expected error code");
                  assert_equals(mediaSource.readyState, "closed", "mediaSource is closed.");
                  assert_equals(mediaSource.sourceBuffers.length, 0, "mediaSource sourceBuffers");
                  assert_equals(mediaSource.activeSourceBuffers.length, 0, "mediaSource activeSourceBuffers");
                  test.done();
              });
          }, "Test removesourcebuffer event firing on sourceBuffers and activeSourceBuffers when MediaSource closes.", { allow_media_element_error: true } );

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              // Basic checks
              assert_equals(mediaSource.sourceBuffers.length, 1, "mediaSource sourceBuffers length");
              assert_equals(mediaSource.sourceBuffers[0], sourceBuffer, "matching sourceBuffer");
              assert_not_equals(undefined, null, "null is different from undefined");

              // SourceBufferList's indexed property getter is anonymous. At least ensure no 'item' is defined.
              assert_equals(mediaSource.sourceBuffers.item, undefined, "'item' is undefined for MediaSource.sourceBuffers");

              // Out-of-range indices should result in undefined.
              assert_equals(mediaSource.sourceBuffers[1], undefined, "undefined result for out-of-range (+1 index)");
              assert_equals(mediaSource.sourceBuffers[2], undefined, "undefined result for out-of-range (+2 index)");
              assert_equals(mediaSource.sourceBuffers[undefined], undefined, "undefined result for out-of-range (undefined index)")
              assert_equals(mediaSource.sourceBuffers[null], undefined, "undefined result for out-of-range (null index)");
              assert_equals(mediaSource.sourceBuffers[""], undefined, "undefined result for out-of-range (empty string index)");
              assert_equals(mediaSource.sourceBuffers["invalid index"], undefined, "undefined result for out-of-range (string index)");

              test.done();
          }, "Test out-of-range SourceBufferList indexed property getter behavior");
        </script>
    </body>
</html>
